package cn.cnsy123.shorturls.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.cnsy123.shorturls.api.controller.param.ShortAddrInsertParam;
import cn.cnsy123.shorturls.api.controller.param.ShortAddrQueryParam;
import cn.cnsy123.shorturls.api.controller.param.ShortAddrUpdateParam;
import cn.cnsy123.shorturls.api.controller.result.ShortAddrQueryResultVo;
import cn.cnsy123.shorturls.api.controller.vo.ShortUrlQueryResult;
import cn.cnsy123.shorturls.api.controller.vo.ShortUrlSaveResult;
import cn.cnsy123.shorturls.business.component.ShortConvertService;
import cn.cnsy123.shorturls.business.entity.ShortAddressService;
import cn.cnsy123.shorturls.business.exception.BusinessException;
import cn.cnsy123.shorturls.business.mapper.ShortAddressServiceMapper;
import cn.cnsy123.shorturls.business.service.ShortUrlAddrService;
@Service
public class ShortUrlAddrServiceImpl implements ShortUrlAddrService {
    
    @Autowired
    private ShortAddressServiceMapper shortAddressServiceMapper;
    @Autowired
    private ShortConvertService shortConvertService;

    @Override
    @Transactional(rollbackFor = BusinessException.class,propagation = Propagation.REQUIRED)
    public ShortUrlSaveResult saveShortUrlAddrInfo(ShortAddrInsertParam shortAddrInsertParam) {
        try {
            ShortAddressService shortAddressService = new ShortAddressService();
            shortAddressService.setCreateTime(new Date(System.currentTimeMillis()));
            shortAddressService.setCreateUser("sys");
            shortAddressService.setDataName(shortAddrInsertParam.getDataName());
            shortAddressService.setDeletedFlag(Boolean.FALSE);
            shortAddressService.setStatus(Boolean.TRUE);
            shortAddressService.setRemarkComment("新增数据");
            shortAddressService.setSourceAddress(shortAddrInsertParam.getSourceUrlAddr());
            shortAddressService.setShortAddress(shortConvertService.shortUrl(shortAddrInsertParam.getSourceUrlAddr()));
            shortAddressService.setSysType(shortAddrInsertParam.getSysType());
            shortAddressService.setUpdateUser("sys");
            shortAddressService.setUpdateTime(new Date(System.currentTimeMillis()));
            int flag = shortAddressServiceMapper.insertSelective(shortAddressService);
            return flag > 0 ? new ShortUrlSaveResult(200, "生成短地址信息成功") : new ShortUrlSaveResult(100, "生成短地址信息失败");
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class,readOnly = true)
    public ShortUrlQueryResult queryShortAddress(ShortAddrQueryParam shortAddrQueryParam) {
        ShortUrlQueryResult shortUrlQueryResult = new ShortUrlQueryResult();
        try {
            List<ShortAddressService> list = shortAddressServiceMapper.selectShortUrlListByPageHelper(shortAddrQueryParam.getDataName(),shortAddrQueryParam.getSysType(),shortAddrQueryParam.getPageNum(),shortAddrQueryParam.getPageSize());
            shortUrlQueryResult.setCode(200);
            if(!CollectionUtils.isEmpty(list)) {
                shortUrlQueryResult.setMsg("查询短地址信息成功");
                shortUrlQueryResult.setData(convertUserQueryData(list));
            }else {
                shortUrlQueryResult.setMsg("暂无短地址信息数据");
                shortUrlQueryResult.setData(null);
            }
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return shortUrlQueryResult;
    }
    
    private List<ShortAddrQueryResultVo> convertUserQueryData(List<ShortAddressService> list){
        List<ShortAddrQueryResultVo> resultVos = new ArrayList<ShortAddrQueryResultVo>();
        Iterator<ShortAddressService> iterator = list.iterator();
        while(iterator.hasNext()) {
             ShortAddressService shortAddressService = iterator.next();
             ShortAddrQueryResultVo resultVo = new ShortAddrQueryResultVo();
             resultVo.setDataId(shortAddressService.getDataId());
             resultVo.setDataName(shortAddressService.getDataName());
             resultVo.setDeletedFlag(shortAddressService.getDeletedFlag());
             resultVo.setShortAddress(shortAddressService.getShortAddress());
             resultVo.setSourceAddress(shortAddressService.getSourceAddress());
             resultVo.setStatus(shortAddressService.getStatus());
             resultVo.setSysType(shortAddressService.getSysType());
             resultVo.setUpdateTime(shortAddressService.getUpdateTime());
             resultVo.setUpdateUser(shortAddressService.getUpdateUser());
             resultVos.add(resultVo);
        }
        return resultVos;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public ShortUrlSaveResult updateShortAddressInfo(ShortAddrUpdateParam shortAddrUpdateParam) {
        ShortUrlSaveResult saveResult = new ShortUrlSaveResult(100,"更新短地址信息失败！");
        try {
            ShortAddressService record = new ShortAddressService();
            record.setDataId(shortAddrUpdateParam.getDataId());
            if(StringUtils.isNotBlank(shortAddrUpdateParam.getDataName()) 
                    ||shortAddrUpdateParam.getStatus() != null
                    ||shortAddrUpdateParam.getDeletedFlag() != null) {
                if(shortAddrUpdateParam.getStatus() != null) {
                    record.setStatus(0 == shortAddrUpdateParam.getStatus().intValue());
                }
                
                if(StringUtils.isNotBlank(shortAddrUpdateParam.getDataName())) {
                    record.setDataName(shortAddrUpdateParam.getDataName());
                }
                
                if(shortAddrUpdateParam.getDeletedFlag() != null) {
                    record.setDeletedFlag((shortAddrUpdateParam.getDeletedFlag().intValue() == 0));
                }
                int flag = shortAddressServiceMapper.updateByPrimaryKeySelective(record);
                if(flag > 0) {
                    saveResult = new ShortUrlSaveResult(200,"更新短地址信息成功！");
                }
            }
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return saveResult;
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public String queryShortAddressItem(String shortUrl) {
        String sourceAddr = "";
        try {
            ShortAddressService shortAddressService = shortAddressServiceMapper.selectByShortUrl(shortUrl);
            if(shortAddressService != null) {
                sourceAddr = shortAddressService.getSourceAddress();
            }
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return sourceAddr;
    }

}
